<template>
  <common-list>
    <common-search slot="search" @search="handleQuery" @reset="resetQuery">
      <el-form
        @submit.native.prevent
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="65px"
      >
        <el-form-item label="应用名称" prop="appName">
          <common-input
            :maxlength="256"
            :verify="false"
            v-model="queryParams.appName"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="URL" prop="url">
          <common-input
            :maxlength="256"
            :verify="false"
            v-model="queryParams.url"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="识别规则" prop="querySensitiveId">
          <el-select v-model="queryParams.querySensitiveId" filterable>
            <el-option label="全部" :value="null"></el-option>
            <el-option
              v-for="item in ruleData"
              :key="item.id"
              :label="item.ruleName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数据分级" prop="queryLevelId">
          <el-select v-model="queryParams.queryLevelId" filterable>
            <el-option label="全部" :value="null"></el-option>
            <el-option
              v-for="item in queryLevelList"
              :key="item.levelId"
              :label="item.name"
              :value="item.levelId"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="数据分类" prop="queryTypeId">
          <el-cascader
            style="width: 100%"
            :options="dataTypeList"
            v-model="queryParams.queryTypeId"
            :show-all-levels="false"
            :props="cascaderProps"
            checkStrictly
            clearable
          >
            <template slot-scope="{ node, data }">
              <common-marquee-text
                :content="data.typeName"
              ></common-marquee-text>
            </template>
          </el-cascader>
        </el-form-item>
        <el-form-item label="重要数据" prop="queryTagId">
          <el-cascader
            style="width: 100%"
            :options="importData"
            v-model="queryParams.queryTagId"
            :show-all-levels="false"
            :props="cascaderProps"
            checkStrictly
            clearable
          >
            <template slot-scope="{ node, data }">
              <common-marquee-text
                :content="data.typeName"
              ></common-marquee-text>
            </template>
          </el-cascader>
        </el-form-item>
        <el-form-item label="核心数据" prop="queryCoreId">
          <el-cascader
            style="width: 100%"
            :options="coreData"
            v-model="queryParams.queryCoreId"
            :show-all-levels="false"
            :props="cascaderProps"
            checkStrictly
            clearable
          >
            <template slot-scope="{ node, data }">
              <common-marquee-text
                :content="data.typeName"
              ></common-marquee-text>
            </template>
          </el-cascader>
        </el-form-item>
      </el-form>
    </common-search>

    <common-table slot="content" v-loading="loading" :data="list">
      <el-table-column
        label="应用名称"
        align="left"
        fixed="left"
        prop="applicationValue"
        show-overflow-tooltip
        width="200px"
      />
      <el-table-column
        label="URL"
        align="left"
        prop="url"
        show-overflow-tooltip
        width="280px"
      />
      <el-table-column
        label="识别规则"
        align="left"
        prop="ruleDataName"
        show-overflow-tooltip
        width="280px"
      />
      <el-table-column
        label="数据分级"
        align="left"
        prop="levelDataName"
        show-overflow-tooltip
        width="160px"
      />
      <el-table-column
        label="数据分类"
        align="left"
        prop="dataTypeName"
        show-overflow-tooltip
      />

      <el-table-column
        label="重要数据"
        align="left"
        prop="tagsDataName"
        show-overflow-tooltip
      />

      <el-table-column
        label="核心数据"
        align="left"
        prop="coreDataName"
        show-overflow-tooltip
      />
      <el-table-column label="操作" align="left" width="60px" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleBatchTrafficAnalysis(scope.row)"
            v-hasPermi="['apisecurity:apiinterface:analysis']"
            >分析
          </el-button>
        </template>
      </el-table-column>
    </common-table>
    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <template v-if="flowAnalysisDiv">
      <flow-analysis
        :path="analysisPath"
        :id="analysisId"
        @changeFlowAnalysisDiv="changeFlowAnalysisDiv"
      />
    </template>
  </common-list>
</template>

<script>
import {
  listInterface,
  queryLevelList,
} from "@/api/apisecurity/apiinterface/interface";
import BaseList from "@/mixins/BaseList.vue";
import FlowAnalysis from "../apiinterface/flowAnalysis.vue";
import * as cAPI from "@/api/datascan/coredata";
import * as iAPI from "@/api/datascan/importdata";
import { listType } from "@/api/datascan/datatype/type";
import { queryList } from "@/api/datahandler/discernRule";

export default {
  name: "index",
  components: {
    FlowAnalysis,
  },
  mixins: [BaseList],
  data() {
    return {
      queryParams: {
        queryCoreId: null,
        querySensitiveId: null,
        queryTagId: null,
        queryTypeId: null,
        queryLevelId: null,
        url: null,
        pageNum: 1,
        pageSize: 10,
        appName: null,
      },
      cascaderProps: {
        label: "typeName",
        value: "typeId",
        emitPath: false,
      },
      ruleData: [],
      coreData: [],
      importData: [],
      dataTypeList: [],
      queryLevelList: [],
      infoRow: {},
      total: 0,
      list: [],
      loading: false,
      date: [],
      flowAnalysisDiv: false,
      analysisPath: null,
      analysisId: null,
    };
  },
  methods: {
    queryLevel() {
      queryLevelList().then((res) => {
        if (res && res.code === 200) {
          this.queryLevelList = res.data.data;
        } else {
          this.queryLevelList = [];
        }
      });
    },
    //获取核心数据
    getCoreDataList() {
      cAPI.queryTreeList().then((res) => {
        if (res && res.code === 200) {
          this.coreData = res.data;
        } else {
          this.coreData = [];
        }
      });
    },
    //获取重要数据
    getImportDataList() {
      iAPI.queryTreeList().then((res) => {
        if (res && res.code === 200) {
          this.importData = res.data;
        } else {
          this.importData = [];
        }
      });
    },
    //获取数据分类
    getDataTypeList() {
      listType().then((res) => {
        if (res && res.code === 200) {
          this.dataTypeList = res.data;
        } else {
          this.dataTypeList = [];
        }
      });
    },
    //获取识别规则
    getRuleData() {
      queryList().then((res) => {
        if (res && res.code === 200) {
          this.ruleData = res.rows;
        } else {
          this.ruleData = [];
        }
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.queryParams = this.$options.data().queryParams;
      this.date = [];
    },
    getList() {
      this.loading = true;
      listInterface(this.queryParams)
        .then((res) => {
          if (res && res.code === 200) {
            this.list = res.rows;
            this.list.forEach((item) => {
              if (
                item.interfaceApplicationList &&
                item.interfaceApplicationList.length
              ) {
                item.applicationValue =
                  item.interfaceApplicationList[0].applicationName;
              } else {
                item.applicationValue = "";
              }

              if (
                item.interfaceDataCoresList &&
                item.interfaceDataCoresList.length
              ) {
                let coreDataName = item.interfaceDataCoresList.reduce(
                  (r, c) => {
                    if (c.coreName) r.push(c.coreName);
                    return r;
                  },
                  []
                );
                item.coreDataName = coreDataName.length
                  ? coreDataName.join(",")
                  : "";
              }

              if (
                item.interfaceSensitiveRuleList &&
                item.interfaceSensitiveRuleList.length
              ) {
                let ruleDataName = item.interfaceSensitiveRuleList.reduce(
                  (r, c) => {
                    if (c.ruleName) r.push(c.ruleName);
                    return r;
                  },
                  []
                );
                item.ruleDataName = ruleDataName.length
                  ? ruleDataName.join(",")
                  : "";
              }
              if (
                item.interfaceDataTagsList &&
                item.interfaceDataTagsList.length
              ) {
                let tagsDataName = item.interfaceDataTagsList.reduce((r, c) => {
                  if (c.tagName) r.push(c.tagName);
                  return r;
                }, []);
                item.tagsDataName = tagsDataName.length
                  ? tagsDataName.join(",")
                  : "";
              }
              if (
                item.interfaceDataLevelList &&
                item.interfaceDataLevelList.length
              ) {
                let levelDataName = item.interfaceDataLevelList.reduce(
                  (r, c) => {
                    if (c.levelName) r.push(c.levelName);
                    return r;
                  },
                  []
                );
                item.levelDataName = levelDataName.length
                  ? levelDataName.join(",")
                  : "";
              }
              if (
                item.interfaceDataTypeList &&
                item.interfaceDataTypeList.length
              ) {
                let dataTypeName = item.interfaceDataTypeList.reduce((r, c) => {
                  if (c.typeName) r.push(c.typeName);
                  return r;
                }, []);
                item.dataTypeName = dataTypeName.length
                  ? dataTypeName.join(",")
                  : "";
              }
            });
            this.total = res.total;
          } else {
            this.list = [];
            this.total = 0;
            this.handleListTips(res);
          }
        })
        .finally(() => (this.loading = false));
    },
    handleInfo(row) {
      this.analysisPath = row.url;
      this.analysisId = row.id;
      this.flowAnalysisDiv = true;
    },
    changeFlowAnalysisDiv() {
      this.flowAnalysisDiv = false;
    },
  },

  created() {
    this.getList();
    this.queryLevel();
    this.getCoreDataList();
    this.getImportDataList();
    this.getDataTypeList();
    this.getRuleData();
  },
};
</script>
<style lang="scss" scoped></style>
